<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); 

class Timetamer
{

/*******************************************************************************
	TimeTaMeR Class ver. 0.1
	Copyright (c) 2007, Dennis T. Kaplan
	All rights reserved.
********************************************************************************
	Redistribution and use in source and binary forms, 
	with or without modification, are permitted provided that the following conditions are met:

	    * Redistributions of source code must retain the above copyright notice, 
	    this list of conditions and the following disclaimer.
	    
	    * Redistributions in binary form must reproduce the above copyright notice, 
	    this list of conditions and the following disclaimer in the documentation and/or 
	    other materials provided with the distribution.
	    
	    * Neither the name of the finalwebsites.com nor the names of its contributors may 
	    be used to endorse or promote products derived from this software without specific 
	    prior written permission.

	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

	_________________________________________________________________________
****************************************************************************
* To get users local time from mysql:
* $this->timetamer->ldate($row->Time);
* To get local time now:
* $this->timetamer->ldate();
****************************************************************************/

	var $time;
	var $abbr;

	function setVars(){
		$this->time = time(); // This is GMT time see config file
		$this->abbr = array(
			'ACST', 'ACT', 'ADDT', 'ADMT', 'ADT', 'AFT', 'AHDT',
			'AHST', 'AKDT', 'AKST', 'AKTST', 'AKTT', 'ALMST',
			'ALMT', 'AMST', 'AMT', 'ANAST', 'ANAT', 'ANT', 'AOT',
			'APT', 'AQTST', 'AQTT', 'ARST', 'ART', 'ASHST', 'ASHT',
			'AST', 'AWT', 'AZOMT', 'AZOST', 'AZOT', 'AZST', 'AZT',
			'BAKST', 'BAKT', 'BDST', 'BDT', 'BEAT', 'BEAUT', 'BMT',
			'BNT', 'BORT', 'BORTST', 'BOST', 'BOT', 'BRST', 'BRT',
			'BST', 'BTT', 'BURT', 'CANT', 'CAPT', 'CAST', 'CAT',
			'CAWT', 'CCT', 'CDDT', 'CDT', 'CEMT', 'CEST', 'CET',
			'CGST', 'CGT', 'CHADT', 'CHAST', 'CHAT', 'CHDT', 'CHOST',
			'CHOT', 'ChST', 'CIT', 'CJT', 'CKHST', 'CKT', 'CLST',
			'CLT', 'CMT', 'COST', 'COT', 'CPT', 'CST', 'CUT',
			'CVST', 'CVT', 'CWT', 'CXT', 'DACT', 'DAVT', 'DDUT',
			'DMT', 'DUSST', 'DUST', 'EASST', 'EAST', 'EAT', 'ECT',
			'EDDT', 'EDT', 'EEST', 'EET', 'EGST', 'EGT', 'EHDT',
			'EIT', 'EPT', 'EST', 'EWT', 'FFMT', 'FJST', 'FJT',
			'FKST', 'FKT', 'FMT', 'FNST', 'FNT', 'FORT', 'FRUST',
			'FRUT', 'GALT', 'GAMT', 'GBGT', 'GEST', 'GET', 'GFT',
			'GHST', 'GILT', 'GMT', 'GMT+1', 'GMT+10', 'GMT+11', 'GMT+12',
			'GMT+2', 'GMT+3', 'GMT+4', 'GMT+5', 'GMT+6', 'GMT+7', 'GMT+8',
			'GMT+9', 'GMT-1', 'GMT-10', 'GMT-11', 'GMT-12', 'GMT-13', 'GMT-14',
			'GMT-2', 'GMT-3', 'GMT-4', 'GMT-5', 'GMT-6', 'GMT-7', 'GMT-8',
			'GMT-9', 'GST', 'GYT', 'HADT', 'HAST', 'HDT', 'HKST',
			'HKT', 'HMT', 'HOVST', 'HOVT', 'HPT', 'HST', 'HWT',
			'ICT', 'IDDT', 'IDT', 'IHST', 'IMT', 'IOT', 'IRDT',
			'IRKST', 'IRKT', 'IRST', 'ISST', 'IST', 'JAVT', 'JDT',
			'JMT', 'JST', 'KART', 'KAST', 'KDT', 'KGST', 'KGT',
			'KIZST', 'KIZT', 'KMT', 'KOST', 'KRAST', 'KRAT', 'KST',
			'KUYST', 'KUYT', 'KWAT', 'LHST', 'LINT', 'LKT', 'LMT',
			'Local ti', 'LONT', 'LRT', 'LST', 'MADMT', 'MADST', 'MADT',
			'MAGST', 'MAGT', 'MALST', 'MALT', 'MART', 'MAWT', 'MDDT',
			'MDST', 'MDT', 'MEST', 'MET', 'MHT', 'MMT', 'MOST',
			'MOT', 'MPT', 'MSD', 'MSK', 'MST', 'MUT', 'MVT',
			'MWT', 'MYT', 'NCST', 'NCT', 'NDDT', 'NDT', 'NEGT',
			'NEST', 'NET', 'NFT', 'NMT', 'NOVST', 'NOVT', 'NPT',
			'NRT', 'NST', 'NUT', 'NWT', 'NZDT', 'NZMT', 'NZST',
			'OMSST', 'OMST', 'ORAST', 'ORAT', 'PDDT', 'PDT', 'PEST',
			'PET', 'PETST', 'PETT', 'PGT', 'PHOT', 'PHST', 'PHT',
			'PKST', 'PKT', 'PMDT', 'PMST', 'PMT', 'PNT', 'PONT',
			'PPMT', 'PPT', 'PST', 'PWT', 'PYST', 'PYT', 'QMT',
			'QYZST', 'QYZT', 'RET', 'RMT', 'ROTT', 'SAKST', 'SAKT',
			'SAMST', 'SAMT', 'SAST', 'SBT', 'SCT', 'SDMT', 'SGT',
			'SHEST', 'SHET', 'SJMT', 'SLST', 'SMT', 'SRT', 'SST',
			'STAT', 'SVEST', 'SVET', 'SWAT', 'SYOT', 'TAHT', 'TASST',
			'TAST', 'TBIST', 'TBIT', 'TBMT', 'TFT', 'TJT', 'TKT',
			'TLT', 'TMT', 'TOST', 'TOT', 'TRST', 'TRT', 'TRUT',
			'TSAT', 'TVT', 'UCT', 'ULAST', 'ULAT', 'URAST', 'URAT',
			'URUT', 'UTC', 'UYHST', 'UYST', 'UYT', 'UZST', 'UZT',
			'VET', 'VLASST', 'VLAST', 'VLAT', 'VOLST', 'VOLT', 'VOST',
			'VUST', 'VUT', 'WAKT', 'WARST', 'WART', 'WAST', 'WAT',
			'WEMT', 'WEST', 'WET', 'WFT', 'WGST', 'WGT', 'WIT',
			'WMT', 'WST', 'YAKST', 'YAKT', 'YDDT', 'YDT', 'YEKST',
			'YEKT', 'YERST', 'YERT', 'YPT', 'YST', 'YWT', 'zzz'
			);
	}
	
	function Timetamer(){
		$CI =& get_instance();
		//$CI->load->library('msg');
		log_message('debug', "TimeTaMeR Class Initialized");
		$tz = $CI->session->userdata('TZ');
		if($tz === FALSE)
		{
			$tz = $CI->input->cookie('TimeTamerTZ');
			if($tz)
			{
				if(strlen($tz) < 6)
				{
					$CI->session->set_userdata(array('TZ' => $tz));
				}
				else
				{
					$this->setVars();
					$this->getRemoteTZ($tz);					
				}
			}
			else
			{
				$CI->tpl['tpljs'][]  = 'TimeTamer';
			}
		}
	}
	
	function setTZ(){
		$CI =& get_instance();
		$tz = $CI->session->userdata('TZ');
		if($tz != putenv('TZ='.$tz)){
			putenv('TZ=GMT');
		}
	}
	
	function getRemoteTZ($cookie)
	{
		log_message('debug', "TimeTaMeR: Getting Remote TZ");
		//$offset = $CI->input->cookie('TimeTamerGMT');
		$cookie = strstr($cookie, 'GMT');
		$cookie = strstr($cookie, '0');
		$cookie = str_replace (' ', '', $cookie);
		$cookie = preg_replace('/[a-z0-9()]/', '', $cookie);
		$tz = $this->checkTZ($cookie);
		if($tz){
			$CI =& get_instance();
			$CI->session->set_userdata(array('TZ' => $tz));
			setcookie('TimeTamerTZ', $tz, time()+60*60*24*365, '/');
			setcookie('TimeTamerGMT', 'Delete', time()-60*60*24*90, '/');
		}
	}
	
	function checkTZ($cookie)
	{
		log_message('debug', "TimeTaMeR: Checking TZ");
		foreach($this->abbr as $v){
			$pos = strpos($cookie, $v);
			if($pos !== FALSE){
				return $v;
			}
		}
		return FALSE;
	}

	function ldate($time = FALSE)
	{
		// We'll remove certain characters for backward compatibility
		// since the formatting changed with MySQL 4.1
		// YYYY-MM-DD HH:MM:SS
		if($time){
			$time = str_replace('-', '', $time);
			$time = str_replace(':', '', $time);
			$time = str_replace(' ', '', $time);
			
			// YYYYMMDDHHMMSS
			$time = mktime(
				substr($time, 8, 2),
				substr($time, 10, 2),
				substr($time, 12, 2),
				substr($time, 4, 2),
				substr($time, 6, 2),
				substr($time, 0, 4)
				);
		}else{
			$time = time();
		}
		$this->setTZ();
		$time = date('r (T)', $time );
		putenv('TZ=GMT');
		return $time;
	}
}
/*

SELECT DISTINCT TZ
FROM `TZ`


SELECT TZ, Abbr, DST, Country
FROM `TZ`
JOIN (
`Country`
) ON ( TZ.ISO3166 = Country.ISO3166 )
ORDER BY `Country`.`Country` ASC
*/
?>
